﻿Imports System.Windows.Forms
Imports WiremetModel.Entities.OEM
Imports WIREMET.DictionaryExtensions

Public Class mdiMain

    Private mvarUzytkownik As clsUzytkownik
    Private biuroCredentials, produkcjaCredentials, statystykaCredentials, zamowieniaCredentials As Uprawienie

    Public Sub New()

        InitializeComponent()
        ts_Biuro.Visible = False
        ts_Produkcja.Visible = False
        ts_Statystyka.Visible = False
        ts_Ciagadla.Visible = False
        ts_SupplyOrders.Visible = False
        mvarUzytkownik = CType(clsUzytkownik.Instance(), clsUzytkownik)
        Me.Text = VAR_CONNECTION_PARAMETERS.DBServerIPAddress & " - " & VAR_CONNECTION_PARAMETERS.DBName & " --> " & VAR_VERSION & "/zalogowany: " & mvarUzytkownik.Nazwisko

        biuroCredentials = User.Role.GetValueOrDefault("biuro")
        produkcjaCredentials = User.Role.GetValueOrDefault("produkcja")
        statystykaCredentials = User.Role.GetValueOrDefault("statystyka")
        zamowieniaCredentials = User.Role.GetValueOrDefault("zamówienia")

        If Not biuroCredentials Is Nothing AndAlso biuroCredentials.WyznModyfikacji AndAlso biuroCredentials.WyznOdczytu Then
            ToolStripSplitButton.Text = "Biuro"
            UprawnieniaBiuro()
        Else
            ToolStripSplitButton.Text = "Produkcja"
            UprawnieniaProdukcja()
        End If

        If mvarUzytkownik.Nazwisko = "PRODUKCJA" Then
            ToolStripSplitButton.Enabled = False
            ts_btn_Produkcja_PowiazanieMaterialu.Enabled = False
            ts_btn_Produkcja_Kontrola.Enabled = False
        End If

        If statystykaCredentials Is Nothing Then
            StatystykiToolStripMenuItem.Visible = False
        End If

        If biuroCredentials Is Nothing OrElse (Not biuroCredentials.WyznOdczytu AndAlso Not biuroCredentials.WyznModyfikacji) Then
            BiuroToolStripMenuItemBiuro.Visible = False
        End If

        If zamowieniaCredentials Is Nothing OrElse (Not zamowieniaCredentials.WyznModyfikacji AndAlso Not zamowieniaCredentials.WyznOdczytu) Then
            ZamówieniaToolStripMenuItem.Visible = False
        End If
    End Sub

    Private Sub UprawnieniaBiuro()

        ToolStripManager.RevertMerge(ts_Main)
        ToolStripManager.Merge(ts_Biuro, ts_Main)

        If Not biuroCredentials.WyznModyfikacji AndAlso biuroCredentials.WyznOdczytu Then

            ts_btn_Biuro_SzukajPracownika.Enabled = False
            ts_btn_Biuro_NoweZlecenie.Enabled = False
            ts_btn_Biuro_SzukajZlecenia.Enabled = False
            ts_btn_Biuro_Klienci.Enabled = False
            ts_btn_Biuro_Material.Enabled = False
            ts_btn_Biuro_Jednostka.Enabled = False
            ts_btn_Biuro_Pakowanie.Enabled = False
            ts_btn_Biuro_Waluta.Enabled = False
            ts_btn_Biuro_FirmaKurierska.Enabled = False
            ts_btn_Biuro_Maszyny.Enabled = False
            ts_btn_Biuro_Magazyn.Enabled = False
        End If

        If mvarUzytkownik.Uprawnienia("wysylka") = 0 Then
            ts_btn_Biuro_SzukajWysylki.Enabled = False
        Else
            ts_btn_Biuro_SzukajWysylki.Enabled = True
        End If

        If Not produkcjaCredentials.WyznModyfikacji AndAlso Not produkcjaCredentials.WyznOdczytu Then
            ts_btn_Biuro_SzukajPracownika.Enabled = False
        Else
            ts_btn_Biuro_SzukajPracownika.Enabled = True
        End If

    End Sub

    Private Sub UprawnieniaProdukcja()

        ToolStripManager.RevertMerge(ts_Main)
        ToolStripManager.Merge(ts_Produkcja, ts_Main)

        If UCase(Environ("ComputerName")) = VAR_COMPUTER_NAME_POMIAROWY Then '
            ts_btn_Produkcja_Kontrola.PerformClick()
        Else
            ts_btn_Produkcja_Zlecenia.PerformClick()
        End If

    End Sub

    Private Sub UprawnieniaCiagadlo()

        ToolStripManager.RevertMerge(ts_Main)
        ToolStripManager.Merge(ts_Ciagadla, ts_Main)

        ts_btn_Ciagadla_Szereg.PerformClick()
    End Sub

    Private Sub UprawnieniaStatystyka()

        ToolStripManager.RevertMerge(ts_Main)
        ToolStripManager.Merge(ts_Statystyka, ts_Main)

    End Sub

    Private Sub UprawnieniaZamówienia()

        ToolStripManager.RevertMerge(ts_Main)
        ToolStripManager.Merge(ts_SupplyOrders, ts_Main)

    End Sub

    Private Sub mdiMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        VAR_ERROR_HANDLER = New clsErrorHandler
        VAR_CONNECTION_PARAMETERS = New clsConnectionParameters

        If Not VAR_CONNECTION_PARAMETERS.IsFullyDefined Then
            With VAR_CONNECTION_PARAMETERS
                .DBName = "wiremet"
                .DBServerIPAddress = "localhost"
                .DBServerPort = "5432"
                .ODBCDriverName = "PostgreSQL Unicode"
                .Password = "postgres"
                .UserName = "postgres"
                .SSLMode = ""
                .TemporaryFolder = ""
                .FileDataBase = ""
                .Save()
            End With
        End If

    End Sub

    Private Sub mdiMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmLogowania.Close()
        SplashScreen.Close()
    End Sub

#Region "Menu1"

    Private Sub tsmiExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

#End Region



    Private Sub ShowMdiChildForm(Of T As {Form, New})(ByRef form As T)
        Try
            If form Is Nothing OrElse form.IsDisposed Then
                Cursor = Cursors.WaitCursor
                form = New T()
                form.MdiParent = Me
                form.Show()
            Else
                form.BringToFront()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub


#Region "Biuro"

    Private formaPlatnosciView As FormaPlatnosciView
    Private frmZlecenia As frmZlecenia
    Private zlecenieBiuroView As ZlecenieBiuroView
    Private wysylkaView As WysylkaView
    Private frmKlientSzukanie As frmKlientSzukanie
    Private frmPracownikSzukaj As frmPracownikSzukaj
    Private frmMaterialSzukanie As frmMaterialSzukanie
    Private gatunekView As GatunekView
    Private frmJednostkaSzukanie As frmJednostkaSzukanie
    Private frmPakowanieSzukanie As frmPakowanieSzukanie
    Private frmWalutaSzukanie As frmWalutaSzukanie
    Private frmFirmaKurierskaSzukanie As frmFirmaKurierskaSzukanie
    Private frmMaszynaSzukanie As frmMaszynaSzukanie
    Private frmMagazynSzukanie As frmMagazynSzukanie
    'Private frmOferty As frmOferty
    Private ofertaView As OfertaView

    Private Sub ts_btn_Biuro_NoweZlecenie_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_NoweZlecenie.Click
        ShowMdiChildForm(frmZlecenia)
    End Sub

    Private Sub ts_btn_Biuro_SzukajZlecenia_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_SzukajZlecenia.Click
        ShowMdiChildForm(zlecenieBiuroView)
    End Sub

    Private Sub ts_btn_Biuro_SzukajWysylki_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_SzukajWysylki.Click
        ShowMdiChildForm(wysylkaView)
    End Sub

    Private Sub ts_btn_Biuro_Klienci_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Klienci.Click
        ShowMdiChildForm(frmKlientSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_SzukajPracownika_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_SzukajPracownika.Click
        ShowMdiChildForm(frmPracownikSzukaj)
    End Sub

    Private Sub ts_btn_Biuro_Material_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Material.Click
        ShowMdiChildForm(frmMaterialSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_Gatunek_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_Biuro_Gatunek.Click
        ShowMdiChildForm(gatunekView)
    End Sub

    Private Sub ts_btn_Biuro_Jednostka_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Jednostka.Click
        ShowMdiChildForm(frmJednostkaSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_Pakowanie_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Pakowanie.Click
        ShowMdiChildForm(frmPakowanieSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_Waluta_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Waluta.Click
        ShowMdiChildForm(frmWalutaSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_FirmaKurierska_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_FirmaKurierska.Click
        ShowMdiChildForm(frmFirmaKurierskaSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_Maszyny_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Maszyny.Click
        ShowMdiChildForm(frmMaszynaSzukanie)
    End Sub

    Private Sub ts_btn_Biuro_Magazyn_Click(sender As Object, e As EventArgs) Handles ts_btn_Biuro_Magazyn.Click
        ShowMdiChildForm(frmMagazynSzukanie)
    End Sub

    'Private Sub ts_btn_Biuro_Oferty_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ts_btn_Biuro_Oferty.Click
    '    ShowMdiChildForm(frmOferty)
    'End Sub

    Private Sub ts_btn_Biuro_SzukajOferty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_btn_Biuro_SzukajOferty.Click
        ShowMdiChildForm(ofertaView)
    End Sub

    Private Sub ts_btn_Biuro_FormyPlatnosci_Click(sender As System.Object, e As System.EventArgs)
        ShowMdiChildForm(formaPlatnosciView)
    End Sub

#End Region

#Region "Produkcja"

    Private ZleceniaTrackingSzukajForm As ZleceniaTrackingSzukajForm
    Private frmPowiazanieMaterialu As frmPowiazanieMaterialu
    Private frmNumerOperacji As frmNumerOperacji
    Private frmKontrolaSzukaj As frmKontrolaSzukaj
    Private frmPoprawki As frmPoprawki
    Private frmTrDrut As frmTrDrut
    Private DrutyBrowserForm As DrutyBrowserForm

    Private Sub ts_btn_Produkcja_Zlecenia_Click(sender As Object, e As EventArgs) Handles ts_btn_Produkcja_Zlecenia.Click
        ShowMdiChildForm(ZleceniaTrackingSzukajForm)
    End Sub

    Private Sub ts_btn_Produkcja_PowiazanieMaterialu_Click(sender As Object, e As EventArgs) Handles ts_btn_Produkcja_PowiazanieMaterialu.Click
        ShowMdiChildForm(frmPowiazanieMaterialu)
    End Sub

    Private Sub ts_btn_Producja_Operacje_Click(sender As Object, e As EventArgs) Handles ts_btn_Producja_Operacje.Click
        ShowMdiChildForm(frmNumerOperacji)
    End Sub

    Private Sub ts_btn_Produkcja_Kontrola_Click(sender As Object, e As EventArgs) Handles ts_btn_Produkcja_Kontrola.Click
        ShowMdiChildForm(frmKontrolaSzukaj)
    End Sub

    Private Sub ts_btn_Produkcja_Poprawki_Click(sender As Object, e As EventArgs) Handles ts_btn_Produkcja_Poprawki.Click
        ShowMdiChildForm(frmPoprawki)
    End Sub

    Private Sub tsbDrut_Click(sender As Object, e As EventArgs) Handles ts_btn_Produkcja_Drut.Click, ts_btn_Statystyka_Drut.Click
        ShowMdiChildForm(frmTrDrut)
    End Sub

    Private Sub ts_btn_DrutyBrowser_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_Produkcja_DrutyBrowser.Click
        ShowMdiChildForm(DrutyBrowserForm)
    End Sub

#End Region

#Region "Statystyka"

    Private frmStatystykiPracownik As frmStatystykiPracownik
    Private analizaView As AnalizaView

    Private Sub ts_btn_Statystyka_Pracownik_Click(sender As Object, e As EventArgs) Handles ts_btn_Statystyka_Pracownik.Click
        ShowMdiChildForm(analizaView)
    End Sub

    Private Sub ts_btn_Statystyka_Pracownika_Click(sender As Object, e As EventArgs) Handles ts_btn_Statystyka_Pracownika.Click
        ShowMdiChildForm(frmStatystykiPracownik)
    End Sub

    Private Sub tsbStatystykaWysylkaWDniu_Click(sender As System.Object, e As System.EventArgs)
        Try
            Dim datePickerDialog As New DateInputDialog()
            If datePickerDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

                Dim report As WysylkaDniaReport = New WysylkaDniaReportService().GenerateReport(datePickerDialog.PickedDate.Date)

                Dim printViewer As New frmPrintViewer(report, False)
                printViewer.WindowState = FormWindowState.Maximized
                printViewer.ShowDialog()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "Ciagadlo"

    Private frmListaSchematow As frmListaSchematow

    Private Sub ts_btn_Ciagadla_Szereg_Click(sender As Object, e As EventArgs) Handles ts_btn_Ciagadla_Szereg.Click
        ShowMdiChildForm(frmListaSchematow)
    End Sub

#End Region

#Region "SupplyOrders"

    Private _supplierView As SupplierView
    Private _wireOrderView As WireOrderView
    Private _dieOrderView As DieOrderView
    Private _miscellaneousOrderView As MiscellaneousOrderView

    Private Sub ts_btn_Zamówienia_Dostawcy_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_SupplyOrders_OpenSupplierView.Click
        ShowMdiChildForm(_supplierView)
    End Sub

    Private Sub ts_btn_SupplyOrders_OpenWireOrderView_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_SupplyOrders_OpenWireOrderView.Click
        ShowMdiChildForm(_wireOrderView)
    End Sub

    Private Sub ts_btn_SupplyOrder_OpenDieOrderView_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_SupplyOrder_OpenDieOrderView.Click
        ShowMdiChildForm(_dieOrderView)
    End Sub

    Private Sub ts_btn_SupplyOrders_OpenMiscellaneousOrderView_Click(sender As System.Object, e As System.EventArgs) Handles ts_btn_SupplyOrders_OpenMiscellaneousOrderView.Click
        ShowMdiChildForm(_miscellaneousOrderView)
    End Sub

#End Region



    Private Sub BiuroToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BiuroToolStripMenuItemBiuro.Click
        ToolStripSplitButton.Text = "Biuro"
        UprawnieniaBiuro()
    End Sub

    Private Sub ProduckjaToolStripMenuItemProducja_Click(sender As Object, e As EventArgs) Handles ProduckjaToolStripMenuItemProducja.Click
        ToolStripSplitButton.Text = "Produkcja"
        UprawnieniaProdukcja()
    End Sub


    Private Sub CiągadlaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CiagadlaToolStripMenuItem.Click
        ToolStripSplitButton.Text = "Ciągadła"
        UprawnieniaCiagadlo()
    End Sub

    Private Sub StatystykiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StatystykiToolStripMenuItem.Click
        ToolStripSplitButton.Text = "Statystyka"
        UprawnieniaStatystyka()
    End Sub
    
    Private Sub ZamówieniaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ZamówieniaToolStripMenuItem.Click
        ToolStripSplitButton.Text = "Zamówienia"
        UprawnieniaZamówienia()
    End Sub

End Class
